package com.hdcircles.autoform.ui.label.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.hdcircles.autoform.R;

import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by lzylzn on 2016/11/22.
 */

public class LocationAdapter extends RecyclerView.Adapter<LocationAdapter.ViewHolder> {

    private List<HashMap<String,String>> locationList;
    private onItemClickListener itemClickListener;

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_location,parent,false));
    }

    public void setLocationList(List<HashMap<String, String>> locationList) {
        this.locationList = locationList;
        notifyDataSetChanged();
    }

    public void setItemClickListener(onItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        holder.tvLocation.setText(locationList.get(position).get("name"));
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                itemClickListener.onItemClick(locationList.get(position));
            }
        });
    }

    @Override
    public int getItemCount() {
        return null==locationList?0:locationList.size();
    }

    public interface onItemClickListener{
        void onItemClick(HashMap<String, String> map);
    }

    static class ViewHolder extends RecyclerView.ViewHolder{

        @BindView(R.id.tv_location)
        TextView tvLocation;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
}
